package com.qiwx.pigeon.Utils;

import android.app.Dialog;
import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiwx.pigeon.R;

import java.util.List;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-24 10:11
 * 创建底部弹出弹框工具类
 **/
public class ActionSheetUtil {

    public interface OnActionSheetSelected {
        void onClick(int whichButton);
    }

    public interface OnActionSheetOptionSelected {
        void onSelected(String content);
    }

    public interface OnActionSheetSelectedZhihuan {
        void onClickZhihuan(int whichButton);
    }

    private ActionSheetUtil() {
    }

    public static Dialog showSelectActionSheet(Context context, List<String> options, String cancel, String title,OnActionSheetOptionSelected listener) {

        final Dialog dlg = new Dialog(context, R.style.ActionSheet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.action_select_sheet, null);
        final int cFullFillWidth = 10000;
        layout.setMinimumWidth(cFullFillWidth);
        TextView tvTitle = (TextView) layout.findViewById(R.id.title);
        View linTitle = layout.findViewById(R.id.lin_title);
        if (title.equals("") || title == null) {
            linTitle.setVisibility(View.GONE);
        } else {
            linTitle.setVisibility(View.VISIBLE);
        }
        tvTitle.setText(title);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        LinearLayout llContainer = layout.findViewById(R.id.llContainer);
        ViewGroup llScroll = layout.findViewById(R.id.llScrollView);
        int count = options.size();
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int maxHeight = ScreenUtils.getScreenHeight(context) / 2;
        int needHeight = (count + 2) * ScreenUtils.dip2px(context, 30);//标题和取消也算高度
        layoutParams.height = maxHeight < needHeight ? maxHeight : needHeight;
        llScroll.setLayoutParams(layoutParams);
        if (options != null && options.size() > 0) {
            for (String option : options) {
                LinearLayout item = (LinearLayout) inflater.inflate(R.layout.item_actionsheet_option, null);
                TextView tvOption = item.findViewById(R.id.tvOption);
                tvOption.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                tvOption.setText(option + "");
                item.setTag(option);
                item.setOnClickListener(v -> {
                    dlg.dismiss();
                    Object tag = v.getTag();
                    if (tag != null && tag instanceof String) {
                        String content = (String) tag;
                        listener.onSelected(content);
                    }
                });
                llContainer.addView(item);
            }
        }
        TextView mCancel = (TextView) layout.findViewById(R.id.cancel);
        mCancel.setText(cancel);
        mCancel.setOnClickListener(v -> dlg.dismiss());
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = -1000;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.BOTTOM;
        w.setAttributes(lp);
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.show();
        return dlg;

    }

}
